/*************
 *#(component)AdminController.java
 *@ztjava.com
 *#(now())
**************/
private static #(component)Service #(firstLowerCase(component))Service = Duang.duang(#(component)Service.class);
/*************
 *修改#(model)
**************/
@UserMethod("#(plugin)#if(plugin!=component)#(component)#end#(model)")
public void addEdit#(model)(){
	Integer id = getParaToInt("id",0);
	#(component)#(model) #(firstLowerCase(component))#(model) = new #(component)#(model)();
	if(id>0){
		#(firstLowerCase(component))#(model) = #(component)#(model).dao.findById(id);
		#if(autocodeModel.list_type=="ztree")
		if(#(firstLowerCase(component))#(model).getInt("parent_id")>0){
				#(component)#(model) parent#(model) = #(component)#(model).dao.findById(#(firstLowerCase(component))#(model).getInt("parent_id"));
				setAttr("parent#(model)", parent#(model));
		}
		#end
	}#if(parent||autocodeModel.list_type=="ztree")else{
		#if(autocodeModel.list_type=="ztree")
			Integer parent_id = getParaToInt("parent_id",0);
			#(firstLowerCase(component))#(model).set("parent_id", parent_id);
			if(parent_id>0){
				#(component)#(model) parent#(model) = #(component)#(model).dao.findById(parent_id);
				setAttr("parent#(model)", parent#(model));
			}
		#end
		#if(parent)
			#(firstLowerCase(component))#(model).set("#(firstLowerCase(parent.code))_id",getParaToInt("#(firstLowerCase(parent.code))_id",0));
		#end
	}#end
	setAttr("#(firstLowerCase(component))#(model)",#(firstLowerCase(component))#(model));
	render("addEdit#(model).html");
}

/*************
 *保存#(model)
**************/
@UserMethod("#(plugin)#if(plugin!=component)#(component)#end#(model)")
public void save#(model)(){
	AuthSession authSession = getAttr(Constant.AUTHSESSION);
	#(component)#(model) #(firstLowerCase(component))#(model) = getModel(#(component)#(model).class);#if(autocodeModel.repeat)
	Ret ret = #(firstLowerCase(component))Service.checkRepeat(#(firstLowerCase(component))#(model),#(component)#(model).tablename
		,#if(parent)" and #(firstLowerCase(parent.code))_id="+#(firstLowerCase(component))#(model).getInt("#(firstLowerCase(parent.code))_id")#else""#end#for(item:columnList)#if(item.repeat)
		,new String[]{"#(item.column)","#(item.repeat)"}#end#end);
	if(ret.get(Constant.RESULT).equals(Constant.ERROR)){
		renderError(ret.get(Constant.MESSAGE).toString());
		return;
	}#end
	saveModel(#(firstLowerCase(component))#(model),authSession.getId());
	#if(autocodeModel.list_type=="ztree")
		if(#(firstLowerCase(component))#(model).getInt("parent_id")>0){
			#(component)#(model) parent = #(component)#(model).dao.findById(#(firstLowerCase(component))#(model).getInt("parent_id"));
			#(firstLowerCase(component))#(model).set("num", parent.getStr("num")+#(firstLowerCase(component))#(model).getInt("id")+",");
		}else {
			#(firstLowerCase(component))#(model).set("num", #(firstLowerCase(component))#(model).getInt("id")+",");
		}
		#(firstLowerCase(component))#(model).update();
		setAttr(Constant.RESULT, Constant.SUCCESS);
		setAttr(Constant.MESSAGE, "保存成功！");
		setAttr("object", #(firstLowerCase(component))#(model));
		renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"object"});
	#else
		renderSuccess();
	#end
}

/*************
 *#(model)列表
**************/
@UserMethod("#(plugin)#if(plugin!=component)#(component)#end#(model)")
public void #(firstLowerCase(model))List(){
	#if(parent)Integer #(firstLowerCase(parent.code))_id = getParaToInt("#(firstLowerCase(parent.code))_id");
	setAttr("#(firstLowerCase(parent.code))_id",#(firstLowerCase(parent.code))_id);#end
	#if(autocodeModel.list_type=="ztree")
	setAttr("listJSON", JsonKit.toJson(#(firstLowerCase(component))Service.get#(model)List(#if(parent)#(firstLowerCase(parent.code))_id#end)));
	render("#(firstLowerCase(model))List.html");
	#else
	if(!getPara("is_ajax","F").equals("T")){
		#if(autocodeModel.list_type=="ztree+table"&&parent)
		setAttr("listJSON", JsonKit.toJson(#(firstLowerCase(component))Service.get#(parent.code)List()));
		#end
		keepPara();
		render("#(firstLowerCase(model))List.html");
		return;
	}

	
	#if(autocodeModel.is_export=="T")
	Page<#(component)#(model)> pd = #(firstLowerCase(component))Service.get#(model)Page(client, getParaMap());
	if(getPara("is_export","F").equals("T")){
		renderFile(ExeclTool.dateToExecl(pd.getList(), ""));
		return;
	}
	renderPageJson(pd);
	#else
	renderPageJson(#(firstLowerCase(component))Service.get#(model)Page(client, getParaMap()));
	#end	
	#end
}

#if(autocodeModel.list_type=="ztree")
/*************
 *删除#(model)
**************/
@UserMethod("#(plugin)#if(plugin!=component)#(component)#end#(model)")
public void delete#(model)(){
	Integer id = getParaToInt("id",0);
	if(!#(firstLowerCase(component))Service.checkChild#(model)(id)){
		renderError("存在子分类不允许删除！");
		return;
	}
	#(component)#(model).dao.deleteById(id);
	setAttr(Constant.RESULT, Constant.SUCCESS);
	setAttr(Constant.MESSAGE, "删除成功！");
	setAttr("id", id);
	renderJson(new String[]{Constant.RESULT,Constant.MESSAGE,"id"});
}
#else
/*************
 *批量删除#(model)
**************/
@UserMethod("#(plugin)#if(plugin!=component)#(component)#end#(model)")
public void batchDelete#(model)(){
	String[] btSelectItem = getParaValues("btSelectItem");
	#if(child)
	if(Db.queryLong("select count(id) from "+#(component)#(child.code).tablename+" where #(model)_id in ("+StringUtil.idsToString(btSelectItem)+")")>0){
		renderError("存在子数据，删除失败！");
		return;
	}
	#end
	#(firstLowerCase(component))Service.batchDelete(#(component)#(model).tablename, btSelectItem);
	renderSuccess();
}
#end